<?php


use Yaf\Controller_Abstract;
use Yaf\Dispatcher;
use Yaf\Loader;

class LoginController extends Controller_Abstract
{

    public function loginAction()
    {
        $postArr = $this->getRequest()->getPost();
        if (!isset($postArr['username']) || !isset($postArr['password'])) {
            return ResponeMessage::clientErrMsg("username，password 不存在!");
        }
        $user = MysqlDb::getDb()->where('username', $postArr['username'])->where('md5Pwd', $postArr['password'])->getOne('account');

        if (empty($user)) {
            return ResponeMessage::clientErrMsg("用户名或者密码不正确!");
        }

        $userModel = new UserModel($user);

        $token = JwtEncrypt::encode($user, 20);

        $expTime = time() + 20 * 3600;
        (new SingleSignModel($userModel))->sava($token, $expTime);

        return ResponeMessage::sucessMsg("登陆成功!", [
            'token' => $token,
            'start_time' => time(),
            'overdue_time' => $expTime,
            'auth' => $user['type'],
            'type' => $userModel->typeToString(),
        ]);
    }


}